<?php
session_start();
    
    // Created By : Julian Ponce
    // B.S. Computer Science - De La Salle Canlubang
    // September 27, 2011
    // IN PARAMETERS : documentId
    // 
    // This AJAX Requests gets all the existing attachments of a Main Document.
    // This is used in viewPageSending.php

// Start - Checker for those users who will just go to the page by typing directly in the url.

if ($_POST) {
    include('../includes/siteConfig.php');
    
    // CONNECT TO DATABASE
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');

    // GET VARIABLES VIA POST METHOD
    $documentId = $_POST['documentId'];
    
    
    $loggedUserId = $_SESSION['loggedUserId'];
    
    // GET CURRENT STEP ID BY CHECKING LOGS
    $query = 
        "
            SELECT stepId, logDateTime
            FROM Log
            WHERE documentId = '$documentId' AND activity = 'Received'
            ORDER BY logDateTime DESC
        ";
     $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
     $row = mysql_fetch_array($result);
     $currentStepId = $row['stepId'];
     $currentLogDateTime = $row['logDateTime'];
    //GET ATTACHMENTS OF THE MAIN DOCUMENT
         $query = 
            "
                SELECT *
                FROM (
                   SELECT * 
                   FROM Log, Attachment
                   WHERE Attachment.mainDocumentId = '$documentId' AND
                         Attachment.attachmentId = Log.documentId AND
                         Attachment.location = '$loggedUserId' AND
                         Log.stepId = '$currentStepId'

                   ORDER BY logDateTime DESC 
                ) as theTable, DocumentSetup
                WHERE theTable.documentSetupId = DocumentSetup.documentSetupId

                GROUP BY documentId
                ORDER BY logDateTime DESC
            ";
         $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    
    
    if (mysql_num_rows($result) > 0) {
        // SAVE QUERY RESULTS TO AN ARRAY > $row. Each row will be placed to a list ($attachmentList).  
        while ($row = mysql_fetch_array($result)) {
            error_log($row['activity']);
            if ( ($row['activity'] == 'Merged' OR $row['activity'] == 'Received' OR $row['activity'] == 'Tagged')
                    AND $row['activity'] != 'Detached' AND $row['activity'] != 'Sent'
                ) {
                $attachmentData = array();
                $attachmentData['attachmentId'] = $row['attachmentId'];
                $attachmentData['documentName'] = $row['documentName'];
                $attachmentData['documentSetupId'] = $row['documentSetupId'];

                $attachmentList[] = $attachmentData;  
            }
            
        }

        echo json_encode($attachmentList);
    }
    
    else {
        
        echo json_encode(0);
    }
    
    mysql_free_result($result);
    mysql_close($connect);
}   // End - Checker for those users who will just go to the page by typing directly in the url.
else {
 echo "You are not authorized to view this page. This incident will be reported immediately.";
}
?>
